
public class Chords implements Runnable{
	public static int chordOffset=60;
	public static Object[] noteArray = new Object[2];
	public void run()
	{
			int[] pitchesArray = toChord(Main.score.changes[1][Main.score.chordPointer%Main.score.changes[0].length]);
			for (int j=0;j<pitchesArray.length;j++)
			{
				if (j==1||j==3||j==2&&pitchesArray.length<4)
				{
//					if (j==pitchesArray.length-1)
//					{
//						noteOut(chordOffset+Main.score.key+pitchesArray[j]+Main.score.changes[0][Main.score.chordPointer],100,Main.score.changes[2][Main.score.chordPointer+1]);
//					}
//					else
//					{
//					System.out.println("test chord out???");
						noteOut(chordOffset+Main.score.key+pitchesArray[j]+Main.score.changes[0][Main.score.chordPointer%Main.score.changes[0].length],100,360);
//					}
				}
			}
			Main.score.chordsDone=true;
		
	}
	
	public static void noteOut(int note, int vel,int onset){
		vel = 70;
		System.out.println("generated chord note:" + (note + Main.score.key)
				+ " " + onset);
		

		boolean bound = false;
		if (Main.noteOut[2]==null) {
			Main.noteOut = new int[3][1];
			Main.noteOut[0][0]=note;
			Main.noteOut[1][0]=vel;
			Main.noteOut[2][0]=onset;
		}
		else {		
			int index=Main.noteOut[2].length-1;
			while (!bound&&index>=0)
			{
				if  (onset>=Main.noteOut[2][index]) {
					bound = true;
				}
				else {
					index--;
				}
			}
			int[][] tempOut = new int[3][Main.noteOut[2].length+1];
			int index2=0;
			while (index2<=index)
			{
				tempOut[0][index2]=Main.noteOut[0][index2];
				tempOut[1][index2]=Main.noteOut[1][index2];
				tempOut[2][index2]=Main.noteOut[2][index2];
				index2++;
			}
			tempOut[0][index2]=note;
			tempOut[1][index2]=vel;
			tempOut[2][index2]=onset;
			while (index2<tempOut[2].length-1)
			{
				tempOut[0][index2+1]=Main.noteOut[0][index2];
				tempOut[1][index2+1]=Main.noteOut[1][index2];
				tempOut[2][index2+1]=Main.noteOut[2][index2];
				index2++;
			}
			Main.noteOut=tempOut;
		}
	}
	public static void waitSym(double wait) // wait in milliseconds
	{
		long waitMS = (long)(wait*60000/(Main.bpm*480));
		try {
			Thread.sleep(waitMS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static int[] toChord(int chordInt)
	{
		String chord = Integer.toBinaryString(chordInt);
		char[] chordCharArray = chord.toCharArray();
		int[] chordArray = new int[chordCharArray.length];
		int j=0;
		for (int i=0;i<chordCharArray.length;i++)
		{
			chordArray[i]=(int) (chordCharArray[chordCharArray.length-i-1]) - 48;
//			System.out.println("chord "+i+" = "+chordArray[i]);
			if (chordArray[i]==1)
			{
				j++;
			}
		}
		int[] pitchesArray = new int[j];
		j=0;
		for (int i=0;i<chordCharArray.length;i++)
		{
			if (chordArray[i]==1)
			{
				pitchesArray[j]=i;
				j++;
			}
		}
		return pitchesArray;
	}
}
